package com.superjson.showpro.dao.impl;

import com.superjson.showpro.base.GenericDaoImpl;
import com.superjson.showpro.entity.Evaluate;
import com.superjson.showpro.dao.EvaluateDao;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

@Repository
public class EvaluateDaoImpl extends GenericDaoImpl<Evaluate> implements EvaluateDao {

    public EvaluateDaoImpl() {
        super(Evaluate.class);
    }

    @Override
    public Evaluate getByTokenIp(Long articleId, String ip) {
        String sql = "SELECT " +
                "  se.* " +
                "FROM " +
                "  sp_evaluate se " +
                "WHERE " +
                "  se.is_deleted = 0 " +
                "AND (se.ip = '" + ip + "' OR se.article_catalog_id = " + articleId + ")" +
                " LIMIT 1";
        Query query = getSession().createSQLQuery(sql).addEntity(Evaluate.class);
        return (Evaluate) query.uniqueResult();
    }
}